package com.neouton.crm.prm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.PageUtil;
import com.neouton.common.util.ResponseAPI;
import com.neouton.crm.prm.entity.ProjectReturnedMoneyVO;
import com.neouton.crm.prm.service.ProjectReturnedMoneyService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目回款表
 *
 * @author
 * @date 2022-12-02 17:03:47
 * @Version 1.0
 */
//@Slf4j
@RestController
@RequestMapping("projectreturnedmoney")
public class ProjectReturnedMoneyController {

    @Resource
    private ProjectReturnedMoneyService projectReturnedMoneyService;

    /**
     * 列表信息查询
     *
     * @param pageNum
     * @param pageSize
     * @param entity
     * @return
     */
    @PostMapping("/queryReturnedMoneyList/{pageNum}/{pageSize}")
    public ResponseAPI queryReturnedMoneyList(@PathVariable(value = "pageNum")Integer pageNum,
                                             @PathVariable(value = "pageSize")Integer pageSize,
                                             @RequestBody ProjectReturnedMoneyVO entity) {
        IPage<ProjectReturnedMoneyVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ProjectReturnedMoneyVO> result = projectReturnedMoneyService.queryReturnedMoneyList(page, entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 查询全部信息
     *
     * @param entity
     * @return
     */
    @PostMapping("/queryAllInfo")
    public ResponseAPI queryAllInfo(@RequestBody ProjectReturnedMoneyVO entity) {
        return ResponseAPI.successData(projectReturnedMoneyService.queryReturnedMoneyList(entity));
    }

    /**
     * 通过日期查询全部信息
     *
     * @param entity
     * @return
     */
    @PostMapping("/queryAllInfoByDate")
    public ResponseAPI queryAllInfoByDate(@RequestBody ProjectReturnedMoneyVO entity) {
        return ResponseAPI.successData(projectReturnedMoneyService.queryAllInfoByDate(entity));
    }

    /**
     * 查询所有信息
     *
     * @param entity
     * @return
     */
    @PostMapping("/queryNowInfo")
    public ResponseAPI queryNowInfo(@RequestBody ProjectReturnedMoneyVO entity) {
        return ResponseAPI.successData(projectReturnedMoneyService.queryNowInfo(entity));
    }

    /**
     * 查询周信息
     *
     * @param entity
     * @return
     */
    @PostMapping("/queryWeekInfo")
    public ResponseAPI queryWeekInfo(@RequestBody ProjectReturnedMoneyVO entity) {
        return ResponseAPI.successData(projectReturnedMoneyService.queryWeekInfo(entity));
    }

    /**
     * 查询月信息
     *
     * @param entity
     * @return
     */
    @PostMapping("/queryMonthInfo")
    public ResponseAPI queryMonthInfo(@RequestBody ProjectReturnedMoneyVO entity) {
        return ResponseAPI.successData(projectReturnedMoneyService.queryMonthInfo(entity));
    }

    /**
     * 查询周信息 始
     *
     * @param entity
     * @return
     */
    @PostMapping("/queryWeekStartInfo")
    public ResponseAPI queryWeekStartInfo(@RequestBody ProjectReturnedMoneyVO entity) {
        return ResponseAPI.successData(projectReturnedMoneyService.queryWeekStartInfo(entity));
    }

    /**
     * 查询周信息 终
     *
     * @param entity
     * @return
     */
    @PostMapping("/queryWeekEndInfo")
    public ResponseAPI queryWeekEndInfo(@RequestBody ProjectReturnedMoneyVO entity) {
        return ResponseAPI.successData(projectReturnedMoneyService.queryWeekEndInfo(entity));
    }

    /**
     * 查询月信息 始
     *
     * @param entity
     * @return
     */
    @PostMapping("/queryMonthStartInfo")
    public ResponseAPI queryMonthStartInfo(@RequestBody ProjectReturnedMoneyVO entity) {
        return ResponseAPI.successData(projectReturnedMoneyService.queryMonthStartInfo(entity));
    }

    /**
     * 查询月信息  终
     *
     * @param entity
     * @return
     */
    @PostMapping("/queryMonthEndInfo")
    public ResponseAPI queryMonthEndInfo(@RequestBody ProjectReturnedMoneyVO entity) {
        return ResponseAPI.successData(projectReturnedMoneyService.queryMonthEndInfo(entity));
    }


    /**
     * 导出数据
     * @param response
     * @param entity
     * @throws Exception
     */
    @PostMapping("/excel")
    public void exportExcel(HttpServletResponse response, @RequestBody ProjectReturnedMoneyVO entity, @RequestHeader("token") String token) throws Exception {
        projectReturnedMoneyService.downExcel(response,entity,token);
    }

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getDetailInfo/{id}")
    public ResponseAPI getDetailInfo(@PathVariable("id") Integer id) {
        ProjectReturnedMoneyVO info = projectReturnedMoneyService.getDetailInfo(id);
        return ResponseAPI.successData(info);
    }

    /**
     * 数据保存
     *
     * @return
     */
    @PostMapping(value = "/save")
    public ResponseAPI save(@RequestBody ProjectReturnedMoneyVO projectReturnedMoney) {
        List<Integer> periodState = projectReturnedMoneyService.queryPeriodState(projectReturnedMoney.getContractNumber());
        if (periodState.contains(projectReturnedMoney.getReturnedMoneyPeriodState())) {
            return ResponseAPI.error("回款期次重复，请重新选择");
        }
        if (projectReturnedMoneyService.verify(projectReturnedMoney)) {
            return ResponseAPI.error("计划回款总额超过合同总金额，请查询后操作");
        }
        projectReturnedMoneyService.saveProjectReturnedMoney(projectReturnedMoney);
        return ResponseAPI.successMes("保存成功");
    }

    /**
     * 项目回款表：数据修改
     */
    @PostMapping("/update")
    public ResponseAPI update(@RequestBody ProjectReturnedMoneyVO projectReturnedMoney) {
        List<Integer> periodState = projectReturnedMoneyService.queryPeriodState(projectReturnedMoney.getContractNumber());
        ProjectReturnedMoneyVO returnedMoneyVO = projectReturnedMoneyService.getDetailInfo(projectReturnedMoney.getBmpId());
        periodState.remove(returnedMoneyVO.getReturnedMoneyPeriodState());
        if (periodState.contains(projectReturnedMoney.getReturnedMoneyPeriodState())) {
            return ResponseAPI.error("回款期次重复，请重新选择");
        }
        projectReturnedMoneyService.updateProjectReturnedMoneyById(projectReturnedMoney);
        return ResponseAPI.successMes("修改模型数据成功");
    }

    /**
     * 删除接口
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public ResponseAPI delete(@PathVariable("id") Integer id) {
        if (id == null) {
            return ResponseAPI.error("参数错误");
        }
            projectReturnedMoneyService.deleteprojectReturnedMoneyById(id);
        return ResponseAPI.successMes("删除数据成功");
    }

    /**
     * 定时发送邮件
     */
    @Scheduled(cron = "0 0 10 * * ?")
    public void scheduled() {
//        log.info("=====>>>>>使用cron  {}",System.currentTimeMillis());
        scheduledSendMail(1);
    }

    public void scheduledSendMail(Integer proType) {
        projectReturnedMoneyService.getScheduleTask();
    }

    /**
     * 定时发消息至待办
     */
//    @Scheduled(cron = "0 0/1 * * * ?")
    public void scheduledMessage() {
        scheduledSendMessage(1);
    }

    public void scheduledSendMessage(Integer proType) {
        projectReturnedMoneyService.getScheduleMessage();
    }

    /**
     * 回显共有数据
     */
    @PostMapping("/queryInfoByContractNumber")
    public ResponseAPI queryInfoByContractNumber(@RequestBody ProjectReturnedMoneyVO projectReturnedMoney) {
        ProjectReturnedMoneyVO projectReturnedMoneyVO = projectReturnedMoneyService.queryInfoByContractNumber(projectReturnedMoney.getContractNumber());
        return ResponseAPI.successData(projectReturnedMoneyVO);
    }

    @PostMapping("/personData")
    public ResponseAPI personData(@RequestBody ProjectReturnedMoneyVO entity) {
        return ResponseAPI.successData(projectReturnedMoneyService.personData(entity));
    }

}
